from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser
from goods.models import SPU, Brand, GoodsCategory
from meiduo_admin.serializers.channels import GoodsCategoriesSerializer
from meiduo_admin.serializers.spus import SpusGoodsSerializer, GoodsBrandsSimpleSerializer


class GoodsCategoriesView(ListAPIView):
    """商品一级分类"""
    permission_classes = [IsAdminUser]
    queryset = GoodsCategory.objects.filter(parent=None)
    serializer_class = GoodsCategoriesSerializer
    pagination_class = None


class GoodsChannelCategoriesView(ListAPIView):
    """商品二级三级分类"""
    permission_classes = [IsAdminUser]
    serializer_class = GoodsCategoriesSerializer
    pagination_class = None

    # def get_queryset(self):
    #     pk = self.request.query_params.get('pk')
    def get(self, request, pk):
        queryset = GoodsCategory.objects.filter(parent=pk)
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)
        # return queryset


class GoodsBrandsSimpleView(ListAPIView, RetrieveAPIView):
    """商品品牌"""
    permission_classes = [IsAdminUser]
    serializer_class = GoodsBrandsSimpleSerializer
    queryset = Brand.objects.all()
    pagination_class = None


class SpusGoodsViewSet(ModelViewSet):
    # spu 管理
    permission_classes = [IsAdminUser]
    # 指定router 动态生成路由时, 提取参数的正则表达式
    lookup_value_regex = r'\d+'
    serializer_class = SpusGoodsSerializer
    queryset = SPU.objects.all()
